package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.StageProcessGetExceptDTO;
import com.xbongbong.paas.pojo.dto.StageProcessVersionDTO;
import com.xbongbong.paas.pojo.vo.StageProcessGetExceptVO;
import com.xbongbong.paas.pojo.vo.StageProcessVersionVO;
import com.xbongbong.paas.service.stage.StageProcessService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 阶段流程
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/stage/process")
public class MobileStageProcessController {
    @Resource
    private StageProcessService stageProcessService;

    /**
     * 流程版本
     * @param stageProcessVersionDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/version", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String stockSearchFilter(@RequestBody @Valid StageProcessVersionDTO stageProcessVersionDTO, BindingResult br) {
        XbbResponse<StageProcessVersionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageProcessVersionVO stageProcessVersionVO = stageProcessService.stageProcessVersion(stageProcessVersionDTO);
                response = new XbbResponse<>(stageProcessVersionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 阶段筛选（级联筛选：版本 + 阶段，默认为当前版本 【不剔除任何】） - bi统计使用
     * @param stageProcessGetExceptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAll(@RequestBody @Valid StageProcessGetExceptDTO stageProcessGetExceptDTO, BindingResult br) {
        XbbResponse<StageProcessGetExceptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageProcessGetExceptVO stageProcessGetExceptVO = stageProcessService.getAll(stageProcessGetExceptDTO);
                response = new XbbResponse<>(stageProcessGetExceptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 【机会专用】阶段筛选（级联筛选：版本 + 阶段，默认为当前版本 【剔除取消】） - 新增类机会统计使用
     * @param stageProcessGetExceptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getExceptCancel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getExceptCancel(@RequestBody @Valid StageProcessGetExceptDTO stageProcessGetExceptDTO, BindingResult br) {
        XbbResponse<StageProcessGetExceptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageProcessGetExceptVO stageProcessGetExceptVO = stageProcessService.getExceptCancel(stageProcessGetExceptDTO);
                response = new XbbResponse<>(stageProcessGetExceptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 【机会专用】阶段筛选（级联筛选：版本 + 阶段，默认为当前版本 【剔除失败、取消】） - 预计类机会统计使用
     * @param stageProcessGetExceptDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getExceptLoseCancel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getExceptLoseCancel(@RequestBody @Valid StageProcessGetExceptDTO stageProcessGetExceptDTO, BindingResult br) {
        XbbResponse<StageProcessGetExceptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StageProcessGetExceptVO stageProcessGetExceptVO = stageProcessService.getExceptLoseCancel(stageProcessGetExceptDTO);
                response = new XbbResponse<>(stageProcessGetExceptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
